_arry = _this select 0;
_payload = _this select 1;
_payloadtype = _this select 2;
//hint format ["%1",_this];

_helo = _arry select 0;
_callsign = _arry select 1;
_heloclass = _arry select 2;
_spwn = _arry select 3;
_heloarr = _arry;

_paycallsign = _payload select 0;
_class = _payload select 1;
_init = _payload select 2;
_payloadWeight = _payload select 3;


//if(_helo getVariable "RUM_inFlight")exitwith{hint "Helo in flight";};

/////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////// Start of Map section //////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////

RUM_ASclick = false;

openMap [true,false];
for [{_n=1},{_n<=3},{_n=_n+1}] do {
	_mkrname = "";
	_type = "";
	_hint = "";
	switch (_n) do {
		case 1: {
			_mkrname = format ["%1target",_helo];
			_type = "mil_pickup";
			_hint = "Select Drop Zone";
		};
		case 2: {
			_mkrname = format ["%1ingress",_helo];
			_type = "mil_start";
			_hint = "Select Drop Zone Ingress Point";
		};
		case 3: {
			_mkrname = format ["%1egress",_helo];
			_type = "mil_end";
			_hint = "Select Drop Zone Egress Point";
		};
		default {
			hint "shit";	
		};
	};
	_asloc = createMarkerLocal [_mkrname, [0,0,0]];
	_asloc setMarkerShapeLocal "ICON";
	_mkrname setMarkerTypeLocal _type;
	_stgtar = format ["'%1' setMarkerPosLocal _pos;RUM_ASclick = true",_mkrname];
	onMapSingleClick _stgtar;
	titleText [_hint, "PLAIN DOWN"];
	waitUntil {RUM_ASclick};
	if((_n == 2) or (_n == 3)) then {
		_mkrname2 = format ["%1target",_helo];
		_mkrname setMarkerDirLocal ((getMarkerPos _mkrname2 select 0)-(getMarkerPos _mkrname select 0)) atan2 ((getMarkerPos _mkrname2 select 1)-(getMarkerPos _mkrname select 1));
	};
	onMapSingleClick "";
	RUM_ASclick = false;
};
titleFadeOut 0;
sleep 1;
openMap [false,false];

/////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////// End of Map section //////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////// Start of Helo Setup //////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////

_distchk2 = true;


// player sidechat format["Array: %1",_arry];
if !("plane" in _heloarr) then {
	if((_payloadWeight >= RUM_SUP_payloadMedHeloLimit))then{
			_heloclass = "CH_47F_EP1"; //this will be the default if the aircraft is to small
		}else{
	_heloclass = _heloclass;
	};
	if((_payloadWeight <= RUM_SUP_payloadParaLimit))then{
			_payloadtype = "Para Drop"; 
		}else{
	_payloadtype = _payloadtype;
	};
};
_homedir = getdir _spwn;
_helosave = _helo;
_helo = createVehicle [_heloclass, getPos _spwn, [], 0, "FLY"];
_helo setVehicleVarName _helosave;
if ("plane" in _heloarr) then {
	RUM_SUP_flyHeightVar = (RUM_SUP_flyHeightPLN +200);
}else{
	RUM_SUP_flyHeightVar = RUM_SUP_flyHeight;
};


_helo setpos [(getpos _spwn select 0),(getpos _spwn select 1),(getpos _spwn select 2)+RUM_SUP_flyHeightVar];
_helo setdir _homedir;
_helo setVariable ["RUM_inFlight", true, true];

_crew = [];
_grp = createGroup side player;
_pilot = _grp createUnit [RUM_SUP_Pilot, getpos _spwn, [], 0, "FORM"];
_helo lockDriver false;
_pilot moveinDriver _helo;
_helo lockDriver true;

_crew = _crew + [_pilot];

_grp2 = createGroup side player;
_cfg=configFile >> "CfgVehicles" >> typeof _helo >> "turrets";
for [{_n=0},{_n<=(count _cfg)},{_n=_n+1}] do {
	_gunner = _grp2 createUnit [RUM_SUP_Pilot, getpos _spwn, [], 0, "FORM"];

	_gunner moveinTurret [_helo,[_n]];
	_helo lockTurret [[_n], true];
	if!(_gunner in crew _helo)then{deleteVehicle _gunner};
	_crew = _crew + [_gunner];
};
_grp2 setCombatMode "RED";
_grp2 setBehaviour "COMBAT";

/////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////// End of Helo Setup //////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////// Start of Supply ///////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////

if ("plane" in _heloarr) then {
	_helo flyInHeight RUM_SUP_flyHeightPLN;
	// set velocity of plane
	_vel = velocity _helo;
	_dir = direction _helo;
	_speed = 200;
	_helo setVelocity [(_vel select 0)+(sin _dir*_speed),(_vel select 1)+(cos _dir*_speed),(_vel select 2)];
}else{
	_helo flyInHeight RUM_SUP_flyHeight;
};

if(_payloadtype == "Sling Load")then{
	_dropitem = createVehicle [_class, [0,0,0], [], 0, "NONE"];

	_dropitem setVehicleInit _init;
	processInitCommands;
	[_helo,_dropitem] execVM (RUM_PATH+"Supply\Sling.sqf");
	["sling",[player,_pilot,_callsign,_paycallsign]] execVM (RUM_PATH+"Supply\radiodialogs.sqf");
} else {
	["para",[player,_pilot,_callsign,_paycallsign]] execVM (RUM_PATH+"Supply\radiodialogs.sqf");
};

sleep 14;

//	
for [{_n=1},{_n<=2},{_n=_n+1}] do {
	_mkrname = "";
	switch (_n) do {
		case 1: {
			_mkrname = format ["%1ingress",_helo];
			[_mkrname,_grp] call RUM_SUP_movewaypoint;
		};
		case 2: {
			_mkrname = format ["%1target",_helo];
			_dz = [_mkrname,_grp] call RUM_SUP_landwaypoint;
		};
	};
};

if(_payloadtype == "Para Drop")then{
	_mkrname = format ["%1egress",_helo];
	[_mkrname,_grp] call RUM_SUP_movewaypoint;
};

_cont = format ["%1",_helo];
RUM_SUP_land = "";
waitUntil {(!alive _helo) or (RUM_SUP_land == _cont)};

if(_payloadtype == "Sling Load")then{

	_height = 4.8;
	_helo flyInHeight _height;
	waitUntil {(!alive _helo) or (getposATL _helo select 2 <= _height) or (getposASL _helo select 2 <= _height)};
	detach _dropitem;
	_dropitem setPos [getpos _dropitem select 0, getpos _dropitem select 1,0];
	_helo setvariable ["lift",false,true];
	_helo FlyInHeight RUM_SUP_flyHeight;
	
}else{

	_heloDropPos = getPos _helo;
	_heloDropDir = getDir _helo;
	_droppos = [(_heloDropPos select 0) - (12 * sin(_heloDropDir)), (_heloDropPos select 1) - (12 * cos(_heloDropDir)), (_heloDropPos select 2)];
	_dropitem = createVehicle [_class, _droppos, [], 0, "NONE"];

	_dropitem setVehicleInit _init;
	processInitCommands;
	
	_chute = "ParachuteMediumWest" createVehicle _droppos;
	_chute setPos _droppos;
	
	// create smoke or IR on chute for tracking while in flight - IR will be visable on the ground as well.
	private ["_marker","_markType"];
	
	_dayTime = daytime;
	
	if ((_dayTime < 19) && (_dayTime > 7)) then {
		_markType = "SmokeShellBlue";
	} else {
		_markType = "NVG_TargetC";
	};

	_marker =  _markType createVehicle position _dropitem;
	_marker setPosATL (getPosATL _dropitem);
	_marker attachTo [_dropitem,[0,0,1]];
	

	_dropitem attachTo [_chute,[0,0,0]];
	_chute setVelocity velocity _helo;
	if(_helo iskindof "Plane")then{
		_helo FlyInHeight RUM_SUP_flyHeightPLN;
	}else{
		_helo FlyInHeight RUM_SUP_flyHeight;
	};
	[_dropitem, _chute, _marker] spawn {
		_dropitem = _this select 0;
		_chute = _this select 1;
		_marker = _this select 2;
		
		// Waiting till on ground
		waitUntil {isNull _chute};
		// waitUntil {sleep 2; (getPosATL _dropitem select 2) < 3};
		detach _dropitem;
		detach _marker;
		sleep 0.3;
		_dropitem setPosATL [(getPosATL _dropitem select 0),(getPosATL _dropitem select 1),0.1];
		_marker setPosATL [(getPosATL _dropitem select 0) + 1,(getPosATL _dropitem select 1) + 1,0.1];

		// adds green smoke for when the object has landed
		if(RUM_SUP_smoke)then{"SmokeShellGreen" createVehicle (getpos _dropitem)};
		
		// Enable R3F compatibility
		_dropitem setVariable ["R3F_LOG_disabled", false];
		
		// Enable PDB saving for MSO
		_id = 1000 + ceil(random(9000));
		_name = format["RUM_log_%1",_id];
		_dropitem setVariable ["pdb_save_name", _name, true];
	};
};

for [{_n=1},{_n<=2},{_n=_n+1}] do {
	_mkrname = "";	
	switch (_n) do {	
		case 1: {
			_mkrname = format ["%1egress",_helo];
			[_mkrname,_grp] call RUM_SUP_movewaypoint;
		};
		case 2: {
			[_mkrname,_grp,_spwn] call RUM_THel_Spawn;
		};
	};
};

_end = format ["%1",_helo];
RUM_SUP_endflight = "";

sleep 1;
waitUntil{(!alive _helo) or (((((getPos _helo select 0) - (getpos _spwn select 0))^2+((getPos _helo select 1) - (getpos _spwn select 1))^2)^0.5) <= 350)};
_crew = crew _helo;
deleteVehicle _helo;
{deleteVehicle _x} forEach _crew;

_mkrname1 = format ["%1ingress",_helo];
_mkrname2 = format ["%1target",_helo];
_mkrname3 = format ["%1egress",_helo];
deletemarker _mkrname1;
deletemarker _mkrname2;
deletemarker _mkrname3;

player sidechat format["%1 has left the AO, ",_helo];

sleep RUM_SUP_ReSupTime;

RUM_SUPAircraft_Spawn = RUM_SUPAircraft_Spawn + [_arry];
_helo setVariable ["RUM_inFlight", false, true];

/////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////// End of Supply //////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////

// exit script
if(true)exitwith{};